package tests;

import common.Utils;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.support.ui.ExpectedConditions;

import java.time.Duration;

public class DetailPage extends Utils {
    public static String url = DetailUrl;
    public DetailPage() {
        super(url);
    }

    public void checkBlogDetail(){
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("body > div.container > div.right > div > div.title")));
        //标题
        String titleText = driver.findElement(By.cssSelector("body > div.container > div.right > div > div.title")).getText();
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("#detail")));
        //内容
        String contentText = driver.findElement(By.cssSelector("#detail")).getText();
        //编辑
        driver.findElement(By.cssSelector("body > div.container > div.right > div > div.operating > button:nth-child(1)"));
        //删除
        driver.findElement(By.cssSelector("body > div.container > div.right > div > div.operating > button:nth-child(2)"));

//        System.out.println("t:" + titleText);
        assert !titleText.isEmpty();
        assert !contentText.isEmpty();
    }

    //博客详情页--编辑功能
    public void checkDetailEdit() {
        //通过编辑按钮进入到编辑页面
        driver.findElement(By.cssSelector("body > div.container > div.right > div > div.operating > button:nth-child(1)")).click();

        //检查跳转是否正确url 更新文章
        String updateUrl = driver.getCurrentUrl();
        String updateText = driver.findElement(By.cssSelector("#submit")).getAttribute("value");

        assert updateUrl.contains("update");
        assert updateText.equals("更新博客");

        wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("#editor > div.editormd-toolbar > div > ul > li:nth-child(39) > a > i")));

        //检查点击更新按钮继续回到博客详情页
        driver.findElement(By.cssSelector("#submit")).click();

        wait.until(ExpectedConditions.alertIsPresent());

        Alert alert = driver.switchTo().alert();
        alert.accept();

        wait.until(ExpectedConditions.urlContains("blog_list"));
    }

    //删除功能
    public void checkDetailDel_dismiss(){
        //当前处于博客列表页,手动进入博客详情
        driver.get(url);
        driver.findElement(By.cssSelector("body > div.container > div.right > div > div.operating > button:nth-child(2)")).click();

        //出现弹窗
        wait.until(ExpectedConditions.alertIsPresent());
        //处理
        Alert alert = driver.switchTo().alert();
        //取消
        alert.dismiss();

        assert (driver.getCurrentUrl()).equals(url);
    }

    public void checkDetailDel_accept(){
        //当前处于博客列表页,手动进入博客详情
        driver.get(url);
        driver.findElement(By.cssSelector("body > div.container > div.right > div > div.operating > button:nth-child(2)")).click();

        //出现弹窗
        wait.until(ExpectedConditions.alertIsPresent());
        //处理
        Alert alert = driver.switchTo().alert();
        //确定
        alert.accept();

        //出现弹窗
        wait.until(ExpectedConditions.alertIsPresent());
        //确定
        alert.accept();

        wait.until(ExpectedConditions.urlContains("blog_list"));
    }

}


